<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${ dataId ? '编辑' : '新增' }-字典分类`"
		width="450px"
		:show-content-loading="showContentLoading"
		:confirm-btn-loading="confirmBtnLoading"
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" :show-message="false" label-width="100px">
			<el-row>
				<el-col :span="22">
					<el-form-item label="编码：" prop="code" class="margin-bottom-10 width-100-percent">
						<jk-input v-model="ruleForm.code" placeholder="编码" @blur="onBlurOfCode" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item label="名称：" prop="name" class="margin-bottom-10 width-100-percent">
						<jk-input v-model="ruleForm.name" placeholder="名称"></jk-input>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item label="排序：" prop="sortNum" class="margin-bottom-10">
						<el-input-number v-model="ruleForm.sortNum" class="width-100-percent" controls-position="right" :min="0" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item label="备注：" prop="remark" class="margin-bottom-10">
						<el-input v-model="ruleForm.remark" type="textarea" :rows="2" placeholder="备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import {
        systemBusinessDictCategoryCreate,
        systemBusinessDictCategoryGet,
        systemBusinessDictCategoryUpdate
    } from '../../../api/basicData/businessDictCategory';
    import { validateCode, validateNull } from '../../../utils/jk/validator';
    export default {
        name: 'SaveDictCategoryDialog',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: Number,
                default: null
            }
        },
        data() {
            return {
                showContentLoading: false,
                confirmBtnLoading: false,
                ruleForm: {
                    id: '',
                    name: '',
                    code: '',
                    sortNum: 1,
                    status: 1,
                    remark: ''
                },
                rules: {
                    code: [{ required: true, validator: validateCode, trigger: 'change' }],
                    name: [{ required: true, validator: validateNull, trigger: 'change' }]
                },
                showDialog: false
            };
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal && this.dataId) {
                    this.showContentLoading = true;
                    systemBusinessDictCategoryGet({ id: this.dataId }).then(res => {
                        if (res.data.status === 200) {
                            this.ruleForm = res.data.res;
                            this.showContentLoading = false;
                        }
                    });
                }
            }
        },
        methods: {
            onBlurOfCode() {
                this.$set(this.ruleForm, 'code', this.ruleForm.code.toUpperCase());
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) delete params.id;
                return params;
            },
            onConfirmBtn() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        const api = !this.dataId ? systemBusinessDictCategoryCreate : systemBusinessDictCategoryUpdate;
                        this.confirmBtnLoading = true;
                        this.$set(this.ruleForm, 'code', this.ruleForm.code.toUpperCase());
                        api(this.getParamData()).then(res => {
                            this.confirmBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$resetForm(this);
                                this.$emit('on-confirm');
                                this.$tipsBar(this, 'save');
                            }
                        });
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                if (!e) {
                    this.$resetForm(this);
                    this.$set(this.ruleForm, 'sortNum', 1);
                    this.ruleForm.remark = '';
                    this.ruleForm.code = '';
                    this.ruleForm.name = '';
                    this.$delete(this.ruleForm, 'id');
                }
                this.$emit('on-visible-change', e);
            }
        }
    };
</script>
